package cn.eeepay.framework.dao;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.AccountAddrOrder;
import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;


public interface AccountAddrOrderDao {

    @SelectProvider(type = SqlProvider.class, method = "addressMonitor")
    @ResultType(AccountAddrOrder.class)
    List<AccountAddrOrder> addressMonitor(@Param("address") String address, @Param("dateStart") String dateStart, @Param("dateEnd") String dateEnd, @Param("page") Page<AccountAddrOrder> page);


    @SelectProvider(type = SqlProvider.class, method = "accountAddressMonitor")
    @ResultType(AccountAddrOrder.class)
    List<AccountAddrOrder> accountAddressMonitor(@Param("address") String address, @Param("dateStart") String dateStart, @Param("dateEnd") String dateEnd, @Param("page") Page<AccountAddrOrder> page);

    @Select("SELECT account_date,SUM(trans_amount) trans_amount,account_address FROM account_addr_order WHERE account_date = #{date} AND mer_province= #{address} GROUP BY account_date,account_address")
    List<AccountAddrOrder> addressMonitorDetail(@Param("address") String address, @Param("date") String date);

    @Select("SELECT account_date,SUM(trans_amount) trans_amount,mer_province FROM account_addr_order WHERE account_date = #{date} AND account_address= #{address} GROUP BY account_date,mer_province")
    List<AccountAddrOrder> accountAddressMonitorDetail(@Param("address") String address, @Param("date") String date);


    @Select("select jump_id from account_addr_order where account_date = #{accountDate} group by jump_id")
    List<AccountAddrOrder> collectOrderByJumpIdAndGroupCode(String accountDate);


    public class SqlProvider {

        public String addressMonitor(final Map<String, Object> param) {
            final String address = (String) param.get("address");
            return new SQL() {
                {
                    SELECT("account_date,jump_id,SUM(trans_amount) trans_amount,mer_province");
                    FROM("account_addr_order");
                    if (StrUtil.isNotBlank(address)) {
                        WHERE("mer_province like concat(#{address},'%')");
                    }
                    WHERE("account_date BETWEEN #{dateStart} and #{dateEnd}");
                    GROUP_BY("account_date,mer_province");
                    ORDER_BY("account_date desc");
                }
            }.toString();
        }


        public String accountAddressMonitor(final Map<String, Object> param) {
            final String address = (String) param.get("address");
            return new SQL() {
                {
                    SELECT("account_date,jump_id,SUM(trans_amount) trans_amount,account_address,mer_province");
                    FROM("account_addr_order");
                    if (StrUtil.isNotBlank(address)) {
                        WHERE("account_address like concat(#{address},'%')");
                    }
                    WHERE("account_date BETWEEN #{dateStart} and #{dateEnd}");
                    GROUP_BY("account_date,account_address");
                    ORDER_BY("account_date desc");
                }
            }.toString();
        }
    }
}
